package com.qiji.video.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qiji.video.bean.Comments;
import com.qiji.video.bean.Users;
import com.qiji.video.bean.vo.CommentAndUsers;
import com.qiji.video.mapper.CommentsMapper;
import com.qiji.video.mapper.UsersMapper;
import com.qiji.video.mapper.extend.CommentsExtendMapper;
import com.qiji.video.service.ICommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl implements ICommentService {

    @Autowired
    private CommentsMapper commentsMapper;
    @Autowired
    private CommentsExtendMapper commentsExtendMapper;
    @Autowired
    private UsersMapper usersMapper;

    @Override
    public PageInfo<? extends Object> selectComments(Integer PageNum, Integer PageSize, Integer id) throws SecurityException{
        PageHelper.startPage(PageNum,PageSize);
        ArrayList<Users> users = new ArrayList<>();
        if (id == null){
            List<Comments> comments = commentsExtendMapper.selectComment();
            for (int i =0;i<comments.size();i++){
                users.add(usersMapper.selectByPrimaryKey(comments.get(i).getFromUid()));
            }
            ArrayList<Object> list = new ArrayList<>();
            list.add(comments);
            list.add(users);
            return new PageInfo<>(list);
        }
        List<Comments> comments = commentsExtendMapper.selectCommentById(id);
        if (comments == null){
            throw new SecurityException("该电影id没有评论");
        }
        for (int i =0;i<comments.size();i++){
            users.add(usersMapper.selectByPrimaryKey(comments.get(i).getFromUid()));
        }
        ArrayList<Object> list = new ArrayList<>();
        list.add(comments);
        list.add(users);
        return new PageInfo<>(list);
    }

    @Override
    public PageInfo<CommentAndUsers> selectCommentById(Integer PageNum, Integer PageSize, Integer id) throws SecurityException{
//        int n = (PageNum-1)*PageSize;
//        int m = PageSize;
        if (id == null){
            PageHelper.startPage(PageNum,PageSize);
            List<CommentAndUsers> list = commentsExtendMapper.selectCommentsNoId();
            return new PageInfo<>(list);
        }
        List<Comments> comments = commentsExtendMapper.selectCommentById(id);
        if (comments == null){
            throw new SecurityException("该电影id没有评论");
        }
        PageHelper.startPage(PageNum,PageSize);
        List<CommentAndUsers> list = commentsExtendMapper.selectComments(id);
        PageInfo<CommentAndUsers> info=new PageInfo<>(list);
        return info;
    }

    @Override
    public void delete(Integer id) throws SecurityException{
        commentsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void delete(Integer[] ids) throws SecurityException{
        int i = ids.length;
        for (int site = 0;site<i;site++) {
            System.out.println(ids[site]);
            if (commentsExtendMapper.selectById(ids[site]) == null){
                throw new SecurityException(" 该评论id"+ids[site]+"不存在");
            }
            commentsMapper.deleteByPrimaryKey(ids[site]);
        }
    }

}
